package com.github.common.util.io;

import com.github.common.util.lang.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件工具类
 */
@Slf4j
public class FileUtil {

    /**
     * 读取文件内容
     *
     * @param filePath 文件路径
     * @return 文件内容
     */
    public static List<String> readLines(String filePath) {
        List<String> lines = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
        } catch (IOException e) {
            throw new RuntimeException("fail to read file", e);
        }
        return lines;
    }

    /**
     * 从resources下面读取文件
     *
     * @param fileName    文件名
     * @param ignoreEmpty 是否忽略空行
     * @return 文件内容
     */
    public static List<String> readLines(String fileName, boolean ignoreEmpty) {
        List<String> lines = new ArrayList<>();
        InputStream is = FileUtil.class.getClassLoader().getResourceAsStream(fileName);

        if (is == null) {
            return lines;
        }
        try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (ignoreEmpty) {
                    if (StringUtil.isEmpty(line)) {
                        continue;//忽略空行
                    }
                }
                lines.add(line);
            }
        } catch (IOException e) {
            throw new RuntimeException("fail to read file", e);
        }
        return lines;
    }

    /**
     * 根据文件路径删除文件,如果是目录则不删除
     *
     * @param filePath 要删除的文件路径
     * @return 删除成功返回true，否则返回false
     */
    public static boolean delFile(String filePath) {
        try {
            Path path = Paths.get(filePath);

            // 检查文件是否存在
            if (!Files.exists(path)) {
                return false;
            }

            // 检查是否是文件而不是目录
            if (!Files.isRegularFile(path)) {
                return false;
            }

            // 删除文件
            Files.delete(path);
            return true;

        } catch (Exception e) {
            throw new RuntimeException("fail to delete file", e);
        }
    }

    /**
     * 检查文件是否存在
     *
     * @param filePath 文件路径
     * @return 文件存在返回true，否则返回false
     */
    public static boolean exists(String filePath) {
        try {
            Path path = Paths.get(filePath);
            return Files.exists(path);
        } catch (Exception e) {
            throw new RuntimeException("fail to check file", e);
        }
    }

    /**
     * 读input stream 到byte[]
     *
     * @param is 输入流
     */
    public static byte[] stream2Byte(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len;
        byte[] b = new byte[1024];
        while ((len = is.read(b, 0, b.length)) != -1) {
            baos.write(b, 0, len);
        }
        return baos.toByteArray();
    }
}
